package org.fjsei.yewu.job.listener;

import org.fjsei.yewu.job.util.Constant;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;

@Slf4j
public class JobHistoryListener implements JobListener {
    @Override
    public String getName() {
        return "JobHistoryListener";
    }

    @Override
    public void jobToBeExecuted(JobExecutionContext jobExecutionContext) {
        log.info("Job about to be executed: Job {}-{}", jobExecutionContext.getJobDetail().getKey().getName(), jobExecutionContext.getJobDetail().getKey().getGroup());
    }

    @Override
    public void jobExecutionVetoed(JobExecutionContext jobExecutionContext) {
        log.info("Job execution vetoed: Job {}-{}", jobExecutionContext.getJobDetail().getKey().getName(), jobExecutionContext.getJobDetail().getKey().getGroup());
    }

    @Override
    public void jobWasExecuted(JobExecutionContext jobExecutionContext, JobExecutionException jobExecutionException) {
        log.info("Job executed: {}-{}. Execution Time: {}", jobExecutionContext.getJobDetail().getKey().getName(), jobExecutionContext.getJobDetail().getKey().getGroup(), Constant.SIMPLE_DATE_FORMAT.format(jobExecutionContext.getFireTime()));
        if (jobExecutionException != null) {
            log.error("Job {}-{} failed: {}", jobExecutionContext.getJobDetail().getKey().getName(), jobExecutionContext.getJobDetail().getKey().getGroup(), jobExecutionException.getMessage(), jobExecutionException);
        }
    }
}
